package com.lanmaoly.cunzheng.threadpoolfour;

import java.util.concurrent.*;

/**
 * 线程池
 *
 * @author fangzhe.hou 2018-9-27 18:02:29
 */
public class TaskExecutePool {

    private static final int size = 5;
    private static final int length = 10;

    public static void main(String[] args) {
        ThreadPoolExecutor pool;
        pool = arrayBlockingQueue();
        for (int i = 0; i < size; i++) {
            pool.execute(() -> {
                for (int j = 0; j < length; j++) {
                    System.out.println(Thread.currentThread().getName() + " 执行 - " + j);
                }
                System.out.println("run");
            });
        }
        pool.shutdown();
    }

    public static ThreadPoolExecutor synchronousQueuePool() {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 3, 3L, TimeUnit.SECONDS, new SynchronousQueue<>(), new ThreadPoolExecutor.AbortPolicy());
        return executor;
    }

    public static ThreadPoolExecutor linkedBlockingQueue() {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 3, 3L, TimeUnit.SECONDS, new LinkedBlockingDeque<>(1), new ThreadPoolExecutor.AbortPolicy());
        return executor;
    }

    public static ThreadPoolExecutor arrayBlockingQueue() {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 3, 3L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1), new ThreadPoolExecutor.AbortPolicy());
        return executor;
    }

}